package com.order.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.order.constant.MessageConstant;
import com.order.constant.StatusConstant;
import com.order.dto.SetmealDTO;
import com.order.dto.SetmealPageQueryDTO;
import com.order.entity.Dish;
import com.order.entity.Setmeal;
import com.order.entity.SetmealDish;
import com.order.exception.DeletionNotAllowedException;
import com.order.exception.SetmealEnableFailedException;
import com.order.mapper.DishMapper;
import com.order.mapper.SetmealDishMapper;
import com.order.mapper.SetmealMapper;
import com.order.result.PageResult;
import com.order.service.SetmealService;
import com.order.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;




    public SetmealVO getByIdDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    public void createSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
//        插入套餐
        setmealMapper.insert(setmeal);

        Long setmealId=setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }
//    分页查询
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum=setmealPageQueryDTO.getPage();
        int pageSize=setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum,pageSize);
        Page<SetmealVO> page= setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {

        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }else {
                setmealMapper.deleteById(id);
                setmealDishMapper.deleteBySetmealId(id);
            }
        }
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        Long id= setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(id);
//        删除套餐关联
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
//        重新插入套餐和菜品的关联
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void setStatus(Integer status, Long id) {
//        判断套餐内餐品是否停售，有则无法起售
        if(status==1){
            List<Dish> dishes=dishMapper.getBySetmealId(id);
            for (Dish dish : dishes) {
                if(dish.getStatus()==0)
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }


}
